<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2020-08-28 16:25
 * Desc: 商城搜索模型
 */

class Model_Global_Module_Malls extends ORM
{
    /**
     * @desc 商品列表
     * @param $params
     * @return array
     */
    public static function search_list($params,$p,$pagesize,$where,$model,$parameters)
    {
        $offset = --$p*$pagesize;
        $where .=" and a.ishidden=0 and status=3 ";

        //总数
        $total = DB::select(DB::expr('count(*) as num '))
            ->from(DB::expr('sline_malls as a'))
            ->where($where)->parameters($parameters)->execute()->get('num');

        $sql = "select a.*,(LENGTH(a.title)-LENGTH(REPLACE(a.title,'{$params['keyword']}',''))) as order_by from sline_malls as a LEFT JOIN `sline_allorderlist` b ON (a.id=b.aid and b.typeid=21) WHERE $where
        ORDER BY a.modtime DESC,a.addtime DESC limit $offset,$pagesize";
        $list = DB::query(1,$sql)->parameters($parameters)->execute()->as_array();
        foreach ($list as &$v)
        {
            $v['price'] = self::get_minprice($v['id'], $v);//价格
            $v['attrlist'] = self::get_attr_list($v['attrid']);//属性
            $v['commentnum'] = Model_Comment::get_comment_num($v['id'], 21); //评论次数
            $v['sellnum'] = Model_Member_Order::get_sell_num($v['id'], 21) + intval($v['bookcount']); //销售数量
            $v['litpic'] = Common::img($v['litpic']);//封面
            $v['satisfyscore']  =St_Functions::get_satisfy(21,$v['id'],$v['satisfyscore'],array('suffix'=>''));
            $v['series'] = St_Product::product_series($v['id'], 21);
            if(Model_Supplier::display_is_open()&&$v['supplierlist'])
            {
                $v['suppliername'] = Arr::get(Model_Supplier::get_supplier_info($v['supplierlist'],array('suppliername')),'suppliername');
            }
        }
        $out = array(
            'total' => $total,
            'list' => $list
        );
        return $out;

    }

    /**
     * @function 获取产品最低价
     * @param $productid
     * @param array $params
     * @return number
     */
    public static function get_minprice($productid, $params = array())
    {
        $time = strtotime(date('Y-m-d'));
        $update_minprice = false;
        if (!is_array($params))
        {
            $params = array('suitid' => $params);
        }
        if (!isset($params['suitid']))
        {
            if (!isset($params['info']))
            {
                $params['info'] = DB::select()->from('malls')->where('id', '=', $productid)->execute()->current();
            }
            if ($params['info']['price_date'] == $time)
            {
                return Currency_Tool::price($params['info']['price']);
            }
            //更新最低价
            $update_minprice = true;
        }
        $where = isset($params['suitid']) ? " AND id=" . $params['suitid'] : '';
        $sql = 'SELECT MIN(price) AS price FROM `sline_malls_suit` WHERE productid=' . $productid . ' and price>0 and (number>0 or number=-1)' . $where;
        $row = DB::query(1, $sql)->execute()->current();
        $price = !empty($row) ? $row['price'] : 0;
        if ($update_minprice)
        {
            DB::update('malls')->set(array('price' => $price, 'price_date' => $time))->where('id', '=', $productid)->execute();
        }
        return Currency_Tool::price($price);
    }
    /**
     * @function 根据属性id返回属性数组.
     * @param $attrid
     * @return mixed
     */
    public static function get_attr_list($attrid,$typeid=null)
    {
        if(empty($attrid))return array();
        $sql = "SELECT id,attrname FROM `sline_malls_attr` WHERE id IN($attrid) AND isopen=1 AND pid!=0  ORDER BY displayorder ASC";
        $arr = DB::query(1,$sql)->execute()->as_array();
        return $arr;
    }

}